package com.definesys.smecrailtransit.module.SiteDetails.dialog;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.databinding.DialogSiteRunBinding;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormPresenter;
import com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter.PopCarePlanAdapter;
import com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter.PopFaultLogAdapter;
import com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter.PopMonthAdapter;
import com.definesys.smecrailtransit.module.MonthForm.bean.CarePlanBean;
import com.definesys.smecrailtransit.module.MonthForm.bean.FaultLogBean;
import com.definesys.smecrailtransit.module.TotalTime.PopWindowAdapter.PopDateTimeAdapter;
import com.definesys.smecrailtransit.module.TotalTime.bean.DateTimeBean;
import com.definesys.smecrailtransit.module.TotalTime.dialog.TotalTimeDialog;
import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.SmecRxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;

import java.util.ArrayList;
import java.util.HashMap;

import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

import static com.definesys.smecrailtransit.module.MonthForm.dialog.MonthFormDialog.GET_CARE_PLAN_SUCCESS;
import static com.definesys.smecrailtransit.module.MonthForm.dialog.MonthFormDialog.GET_FAULT_LOG_SUCCESS;

/**
 * Created by xupeizuo on 2017/8/29.
 */

public class SiteRunDialog extends Dialog {
    public static final String CHANGE_POP_MONTH = "changePopMonthSRD(Object o)";
    private Context mContext;
    private DialogSiteRunBinding dialogSiteRunBinding;
    private int currentPage=0;
    private PopDateTimeAdapter popDateTimeAdapter;
    private PopFaultLogAdapter popFaultLogAdapter;
    private PopCarePlanAdapter popCarePlanAdapter;
    private MonthFormPresenter monthFormPresenter;
    private PopMonthAdapter popMonthAdapter;
    protected Bus mRxBus;
    private String eleNo;
    private String eleContractNo;
    private String selectMonth;

    public void setEleContractNo(String eleContractNo) {
        this.eleContractNo = eleContractNo;
    }

    private HashMap<String, Object> param = new HashMap<>();
    public void setEleNo(String eleNo) {
        this.eleNo = eleNo;
        if(currentPage==0){
            dialogSiteRunBinding.error.setName(eleNo);
        }else {
            dialogSiteRunBinding.errorTime.setName(eleNo);
        }
        dialogSiteRunBinding.invalidateAll();
    }

    public SiteRunDialog(Context context, int themeResId, int page) {
        super(context,themeResId);
        mContext=context;
        this.currentPage=page;
        monthFormPresenter=new MonthFormPresenter(mContext);
        this.setCanceledOnTouchOutside(true);
        mRxBus= SmecRxBus.get();
        mRxBus.register(this);
        dialogSiteRunBinding= DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.dialog_site_run,null,false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialogSiteRunBinding.getRoot());
        _initView();
    }

    private void _initView(){
        ArrayList<String> list = new ArrayList<>();
        list.add("一个月");
        list.add("三个月");
        list.add("半年");
        popMonthAdapter = new PopMonthAdapter(list,mRxBus, SmecRailApi.MonthCode.SITEDETAILS);
        dialogSiteRunBinding.error.monthList.setItemAnimator(new SlideInLeftAnimator());
        dialogSiteRunBinding.error.monthList.setLayoutManager(new LinearLayoutManager(mContext));
        dialogSiteRunBinding.error.monthList.setAdapter(popMonthAdapter);

        if(currentPage == 1){
            popDateTimeAdapter=new PopDateTimeAdapter(mContext.getResources().getDisplayMetrics());
            dialogSiteRunBinding.time.setVisibility(View.VISIBLE);
            dialogSiteRunBinding.content.setVisibility(View.GONE);
            dialogSiteRunBinding.errorTime.dateTimeList.setLayoutManager(new LinearLayoutManager(mContext));
            dialogSiteRunBinding.errorTime.dateTimeList.setAdapter(popDateTimeAdapter);
        }
        if(currentPage == 0){
            popFaultLogAdapter=new PopFaultLogAdapter();
            popFaultLogAdapter.start = 1;
            dialogSiteRunBinding.error.faultLogList.setLayoutManager(new LinearLayoutManager(mContext));
            dialogSiteRunBinding.error.faultLogList.setAdapter(popFaultLogAdapter);
            popCarePlanAdapter=new PopCarePlanAdapter();
            popCarePlanAdapter.start = 1;
            dialogSiteRunBinding.error.carePlanList.setLayoutManager(new LinearLayoutManager(mContext));
            dialogSiteRunBinding.error.carePlanList.setAdapter(popCarePlanAdapter);
            dialogSiteRunBinding.error.popMonthImg.setBackgroundResource(R.drawable.month_form_bottom);
            dialogSiteRunBinding.error.faultLogBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialogSiteRunBinding.error.faultLogBtn.setTextColor(mContext.getResources().getColor(R.color.customWhite));
                    dialogSiteRunBinding.error.carePlanBtn.setTextColor(mContext.getResources().getColor(R.color.monthForm_title));
                    dialogSiteRunBinding.error.faultLog.setVisibility(View.VISIBLE);
                    dialogSiteRunBinding.error.carePlan.setVisibility(View.GONE);
                }
            });
            dialogSiteRunBinding.error.carePlanBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    param.put("eleContractNo",eleContractNo);
                    monthFormPresenter.getCarePlan(param);
                    dialogSiteRunBinding.error.carePlanBtn.setTextColor(mContext.getResources().getColor(R.color.customWhite));
                    dialogSiteRunBinding.error.faultLogBtn.setTextColor(mContext.getResources().getColor(R.color.monthForm_title));
                    dialogSiteRunBinding.error.faultLog.setVisibility(View.GONE);
                    dialogSiteRunBinding.error.carePlan.setVisibility(View.VISIBLE);
                }
            });
            dialogSiteRunBinding.error.changePopMonth.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(dialogSiteRunBinding.error.popMonthBg.getVisibility() == View.VISIBLE){
                        dialogSiteRunBinding.error.popMonthBg.setVisibility(View.GONE);
                        dialogSiteRunBinding.error.popMonthImg.setBackgroundResource(R.drawable.month_form_bottom);

                    }else{
                        dialogSiteRunBinding.error.popMonthBg.setVisibility(View.VISIBLE);
                        dialogSiteRunBinding.error.popMonthImg.setBackgroundResource(R.drawable.month_form_top);
                    }
                }
            });


        }


    }

    /**
     * 获取电梯运行时长成功
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(TotalTimeDialog.GET_ELEVATOR_TIME_SUCCESS)
            }
    )
    public void getElevatorTimeSuccess(ArrayList<DateTimeBean> list){
        if(popDateTimeAdapter == null){
            return;
        }
        popDateTimeAdapter.setList(list);
        popDateTimeAdapter.notifyDataSetChanged();
    }

    /**
     * 获取保养计划成功
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_CARE_PLAN_SUCCESS)
            }
    )
    public void getCarePlanSuccess(ArrayList<CarePlanBean> list){
        if(popCarePlanAdapter == null){
            return;
        }
        popCarePlanAdapter.setList(list);
        popCarePlanAdapter.notifyDataSetChanged();
    }

    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_FAULT_LOG_SUCCESS)
            }
    )
    public void getFaultLogSuccess(FaultLogBean faultLogBean){
        if(popFaultLogAdapter == null){
            return;
        }
        dialogSiteRunBinding.error.setCount(faultLogBean.getFault().size()+"");
        popFaultLogAdapter.setList(faultLogBean.getFault());
        dialogSiteRunBinding.invalidateAll();
    }

    /**
     * 修改站点月份
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(CHANGE_POP_MONTH)
            }
    )
    public void changePopMonth(Object o){
        dialogSiteRunBinding.error.popMonth.setText((String)o);
        selectMonth = (String)o;
        dialogSiteRunBinding.error.popMonthBg.setVisibility(View.GONE);
        dialogSiteRunBinding.error.popMonthImg.setBackgroundResource(R.drawable.month_form_bottom);
        getFaultLog(eleContractNo);
    }

    //获取某一电梯的故障日志
    public void getFaultLog(String eleContractNo){
        //设置参数
        HashMap<String, Object> param = new HashMap<>();
        switch (selectMonth){
            case "一个月": param.put("month",1);break;
            case "三个月": param.put("month",3);break;
            case "半年": param.put("month",6);break;
            default:param.put("month",1);
        }
        param.put("eleContractNo",eleContractNo);

        //网络请求
        this.monthFormPresenter.getFaultLog(param);
    }
}
